package com.mediacloud.app.newsmodule.view;

import com.mediacloud.app.assembly.banner.NetImageBanner4Component;
import com.mediacloud.app.assembly.views.NetImageViewX;
import com.mediacloud.app.newsmodule.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
/**
 * 加载完成后根据屏幕宽度来等比缩放的
 * @author x
 *
 */
public class ResizeLoadImageBanner extends NetImageBanner4Component {

	public ResizeLoadImageBanner(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public ResizeLoadImageBanner(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	@Override
	public void addItem(String imageUrl, CharSequence title,final Drawable defaultRes)
	{
		if (mItems == null)
			return;
		final NetImageViewX image = new BannerImageResize(getContext());
		image.defaultBackGroundColor=defaultLoadBackground;
		image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
		if(defaultRes!=null)
		{
			image.defaultDrawable=defaultRes;
			image.setDefaultRes();
		}
		else
		{
			image.defaultRes=R.drawable.default_thumbnail_banner;
			image.setImageResource(R.drawable.default_thumbnail_banner);
		}
		if(!TextUtils.isEmpty(imageUrl))
			image.load(imageUrl);
		mItems.add(new ImageItem(image, title, null));
	}
	protected class BannerImageResize extends NetImageViewX4VR
	{

		public BannerImageResize(Context context, AttributeSet attrs,
				int defStyle) {
			super(context, attrs, defStyle);
		}

		public BannerImageResize(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		public BannerImageResize(Context context) {
			super(context);
		}

		@Override
		public void onLoadingComplete(String arg0, View view, Bitmap bitmap)
		{
				loaded=true;
				if(bitmap==null)
					return;
				float w=bitmap.getWidth();
				float h=bitmap.getHeight();
				setBackgroundColor(Color.TRANSPARENT);
				setAdjustViewBounds(true);
				setScaleType(ScaleType.FIT_XY);
				float delta= h/w;
				int bannerHeight=(int) (getResources().getDisplayMetrics().widthPixels*delta);
				defaultHeight=bannerHeight;
				updateLayoutParam();
		}
	}

}